package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

@Slf4j
@Service
@Transactional
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;


    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //准备分页对象
        Page<Dish> page = new Page<>(pageNum, pageSize);
        //StringUtils.isNotEmpty(name) 判断name是否为非空字符串
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        page = dishMapper.selectPage(page, wrapper);
        //2 遍历菜品列表
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3. 根据菜品的分类id从分类表中查询分类信息(名称)
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
                //4. 根据菜品id从口味表查询当前菜品口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }

        return page;
    }

    @Override
    public void save(Dish dish) {
        //1. 向dish表保存基本信息,(mybatisplus自动返回主键)
        dishMapper.insert(dish);
        log.info("保存之后的菜品主键:{}", dish.getId());

        //2. 遍历前端传入的口味列表信息
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //3为口味设置所属菜品id
                dishFlavor.setDishId(dish.getId());
                //4. 保存口味
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //1. 从菜品表根据id查询
        Dish dish = dishMapper.selectById(id);

        //2. 根据菜品的分类id从分类表中查询分类信息(名称)
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());

        //3. 根据菜品id从口味表查询当前菜品口味列表
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);

        return dish;
    }

    @Override
    public void updateDish(Dish dish) {
        //1. 根据菜品id直接更新菜品基本信息
        dishMapper.updateById(dish);

        //2. 删除口味表中当前菜品的相关信息
        //delete from dish_flavor where dish_id = #{}
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        System.out.println(dish.getId());
        dishFlavorMapper.delete(wrapper);

        //3. 将修改之后口味列表重新保存到口味表中
        List<DishFlavor> dishFlavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavorList)) {
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public ResultInfo updateDishStatus(int status, Long[] ids) {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        for (int i = 0; i < ids.length; i++) {
            hashSet.add(dishMapper.selectById(ids[i]).getStatus());
        }
        System.out.println(hashSet.size());
        System.out.println(ids.length);
        if (hashSet.size() <= 1) {
            if (status == 0) {
                for (Long id : ids) {
                    Dish dish = dishMapper.selectById(id);
                    dish.setStatus(0);
                    dishMapper.updateById(dish);
                }
                return ResultInfo.success(null);
            } else {
                for (Long id : ids) {
                    Dish dish = dishMapper.selectById(id);
                    dish.setStatus(1);
                    dishMapper.updateById(dish);
                    return ResultInfo.success(null);
                }
            }
        } else {
            return ResultInfo.error("有状态不同的菜品,不能改变状态!");
        }

        return null;
    }

    @Override
    public ResultInfo deleteDish(String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            if (dishMapper.selectById(ids[i]).getStatus() == 0) {
                return ResultInfo.error("商品在售不能删除!");
            }
        }
        for (int i = 0; i < ids.length; i++) {
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            Dish dish = dishMapper.selectById(ids[i]);
            wrapper.eq(Dish::getId, dishMapper.selectById(ids[i]).getId());
            dishMapper.delete(wrapper);

            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, dish.getId());
            dishFlavorMapper.delete(wrapper1);
        }
        return ResultInfo.success(null);
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        //select * from dish where categoryId = #{categoryId} and status = 1
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1);//上架

        //菜品集合
        List<Dish> dishList = dishMapper.selectList(wrapper);
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //根据菜品的id从口味表中查询菜品口味信息
                //select * from dish_flavor where dish_id = #{dish.id}
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<DishFlavor>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());

                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }

        return dishList;
    }

    @Override
    public List<Dish> findByName(String name) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Dish::getName, name)
                .eq(Dish::getStatus, 1);//上架
        List<Dish> dishList = dishMapper.selectList(wrapper);
        return dishList;
    }

}
